package org.jeecg.modules.demo.property.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import liquibase.pro.packaged.L;
import org.jeecg.modules.demo.property.entity.ZnzfComplaint;
import org.jeecg.modules.demo.property.entity.ZnzfComplaintParam;
import org.jeecg.modules.demo.property.mapper.ZnzfComplaintMapper;
import org.jeecg.modules.demo.property.service.IZnzfComplaintService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 投诉建议
 * @Author: jeecg-boot
 * @Date:   2022-03-29
 * @Version: V1.0
 */
@Service
public class ZnzfComplaintServiceImpl extends ServiceImpl<ZnzfComplaintMapper, ZnzfComplaint> implements IZnzfComplaintService {

    @Resource
    private ZnzfComplaintMapper znzfComplaintMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysDepartMapper sysDepartMapper;

    @Override
    public List<ZnzfComplaint> getBySubmiter(String submiter) {
        LambdaQueryWrapper<ZnzfComplaint> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfComplaint::getSubmiter, submiter);
        List<ZnzfComplaint> znzfComplaintList = znzfComplaintMapper.selectList(queryWrapper);
        return znzfComplaintList;
    }

    /**
     * 添加一条投诉建议
     * @param znzfComplaintParam
     * @return
     */
    @Override
    public void addComplaint(ZnzfComplaintParam znzfComplaintParam) {
        ZnzfComplaint znzfComplaint = new ZnzfComplaint();
        znzfComplaint.setTitle(znzfComplaintParam.getTitle());
        znzfComplaint.setContent(znzfComplaintParam.getContent());
        znzfComplaint.setType(znzfComplaintParam.getType());
        znzfComplaint.setSubmiter(znzfComplaintParam.getSubmiter());
        znzfComplaintMapper.insert(znzfComplaint);
    }

    /**
     * 查找所有投诉信息
     * @return
     */
    @Override
    public List<ZnzfComplaint> queryAllComplaint() {
        LambdaQueryWrapper<ZnzfComplaint> queryWrapper = new LambdaQueryWrapper<>();
        List<ZnzfComplaint> resList = new ArrayList<>();
        List<ZnzfComplaint> znzfComplaintList = znzfComplaintMapper.selectList(queryWrapper);
        for (int i = 0; i < znzfComplaintList.size(); i++) {
            ZnzfComplaint znzfComplaint = znzfComplaintList.get(i);
            SysUser sysUserSubmit = sysUserMapper.selectById(znzfComplaint.getSubmiter());
            SysDepart depart = null;
            if (sysUserSubmit != null) {
                znzfComplaint.setUserName(sysUserSubmit.getRealname());
                depart = sysDepartMapper.selectById(sysUserSubmit.getDepartIds());
            }
            if (depart != null) {
                znzfComplaint.setDepartName(depart.getDepartName());
            }
            if (znzfComplaint.getResponser() != null) {
                SysUser sysUserResponser = sysUserMapper.selectById(znzfComplaint.getResponser());
                if (sysUserResponser != null) {
                    znzfComplaint.setResponserName(sysUserResponser.getRealname());
                }
            }
            resList.add(znzfComplaint);
        }
        return resList;
    }
}
